<?php
/**
 * Description of MediaDAO
 *
 * @author Magno
 */
class MediaDAO {

    private $conexao;
    public function __construct() {
        $this->conexao = Conexao::getConexao();
    }

    public function salvar($media) {
        
        try {

            if($media->getMediaID() != null && $media->getMediaID() > 0) {
                $statment = $this->conexao->prepare("UPDATE media SET
                        caminho = :caminho
                        WHERE mediaID = :mediaID");

            }else {
                $statment = $this->conexao->prepare("INSERT INTO media(mediaID,caminho) VALUES (:mediaID,:caminho);");
            }

            if($statment->execute($media->toBD())) {

                if($media->getMediaID() == null || $media->getMediaID() <= 0)
                    $media->setMediaID($this->conexao->lastInsertId());

                return $media;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro MediaDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro MediaDAO:".$err->getMessage());
        }

    }

    public function excluir($mediaID) {

        try {

            $statment = $this->conexao->prepare("DELETE FROM media WHERE mediaID = :mediaID");
            $statment->bindParam(':mediaID',$mediaID);

            if(!$statment->execute()) {
                $erros = $statment->errorInfo();
                throw new Exception("Erro MediaDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro MediaDAO:".$err->getMessage());
        }

    }

    public function listar($pagina = 0,$caminho = "") {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = $this->getWhere($caminho);
            $medias = array();
            $statment = $this->conexao->query( "SELECT * FROM media e $where $limite" );

            foreach ( $statment->fetchAll(PDO::FETCH_SERIALIZE) as $row )
                $medias[] = new MediaBean($row['mediaID'], $row['caminho']);

            return $medias;

        }catch(PDOException $err) {
            throw new Exception("Erro MediaDAO:".$err->getMessage());
        }
    }

    public function buscarPorID($mediaID) {
        try {

            $row = $this->conexao->query( "SELECT * FROM media WHERE mediaID = $mediaID " )->fetch(PDO::FETCH_SERIALIZE);

            if(isset($row['mediaID']) && $row['mediaID'] > 0) {
                $media = new MediaBean($row['mediaID'], $row['caminho']);
                return $media;
            }else {
                return null;
            }

        }catch(PDOException $err) {
            throw new Exception("Erro MediaDAO:".$err->getMessage());
        }
    }

    private function getWhere($caminho = "") {
        $where = " WHERE 1 = 1 ";

        if (isset ($caminho) && strlen($caminho) > 0) {
            $where = $where." AND e.caminho = '$caminho' ";
        }

        return $where;
    }

    public function total($caminho = "") {
        try {

            $where = $this->getWhere($caminho);
            $total = $this->conexao->query( "SELECT COUNT(e.mediaID) total FROM media e $where" )->fetch(PDO::FETCH_COLUMN);
            return $total;

        }catch(PDOException $err) {
            throw new Exception("Erro MediaDAO:".$err->getMessage());
        }
    }

}
?>
